//引入常量
import {PLUS_COUNT,UPDATE,ADDITEM,REDUCE_COUNT,SUBCART,ADDCITY,CKSEL,CLEARPLACE} from "./constant";

export default{
    // 对状态操作
    [PLUS_COUNT](state,id){
        state.cartList.map(el=>{
            if(el.item.id === id){
                el.num++;
            }
        })
    },
    [REDUCE_COUNT](state,id){
        state.cartList.forEach(el=>{
            if(el.item.id == id){
              if(el.num<=1) return;
              el.num--;
            }
        })
    },
    [UPDATE](state){
        state.title = "我是修改后的数据";
    },
    [ADDITEM](state,item){
        let rel = true;
        state.cartList.forEach(el=>{
            console.log(el.item.id)
            if(el.item.id == item.id){
                el.num++;
                rel = false;
            }
        }) 
        if(rel){
            state.cartList.push({item,num:1,checked:false})
        }
    },
    [ADDCITY](state,item){
        state.cityList.push(item)
    },
    [SUBCART](state){
        state.cartList.forEach((el)=>{
            if(el.checked){
                state.placeOrder.push(el);
            }
        })
    },
    [CKSEL](state,item){
        state.cartList.forEach(el=>{
            if(el.item.id == item.item.id){
                el.checked = !el.checked
            }
        })
    },
    [CLEARPLACE](state){
        state.placeOrder = [];
    }
}